/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.ugame.utility;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author user
 */
public class Security {

    private static Security instance;
    private static byte[] iv = {0x0a, 0x01, 0x02, 0x03, 0x04, 0x0b, 0x0c, 0x0d};

    public static Security getInstance() {
        if (instance == null) {
            instance = new Security();
        }
        return instance;
    }

    public String getMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);

            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException ex) {
        }
        return null;
    }

    public String getPasswordMD5(String firstInput, String secondInput) {
        String usernameMD5 = getMD5(firstInput);
        String passwordMD5 = getMD5(secondInput);
        String temp = usernameMD5.substring(0, usernameMD5.length() / 2) + passwordMD5.substring(0, passwordMD5.length() / 2);
        return getMD5(temp);
    }

    public String createFirstSecretKey(String key) {      
        Date date = new Date();
        Date now = new Date(date.getYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes());
        return key+":ugame:"+now.getTime();
    }
    
    public String createSecondSecretKey(String key) {      
        Date date = new Date();
        Date now = new Date(date.getYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes()-1);
        return key+":ugame:"+now.getTime();
    }

    public String encrypt(String plainText, String key) {
        try {
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            //declare iv
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            //declare secretKey
            DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(keySpec);
            //create bytes of plainText
            byte[] bytes = plainText.getBytes("UTF-8");
            //encrypt
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
            byte[] encrypted = cipher.doFinal(bytes);
            //return cipherText
            BASE64Encoder base64encoder = new BASE64Encoder();
            String cipherText = base64encoder.encode(encrypted);
            return cipherText;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public String decrypt(String cipherText, String key) {
        try {
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            //declare iv
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            //declare secretKey
            DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(keySpec);
            //create bytes of cipherText
            BASE64Decoder base64decoder = new BASE64Decoder();
            byte[] bytes = base64decoder.decodeBuffer(cipherText);
            //decrypt
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
            byte[] decrypted = cipher.doFinal(bytes);
            //return plainText
            String plainText = new String(decrypted, "UTF-8");
            return plainText;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
